package com.bourse.core.ibatis;

import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class SqlMapClientFactoryImpl implements SqlMapClientFactory {

	private String configuration;
	private Properties connectionProperties;

	
	/**
	 * @param configuration the configuration to set
	 */
	public void setConfiguration(String configuration) {
		this.configuration = configuration;
	}

	
	/**
	 * @param connectionProperties the new connection properties
	 */
	public void setConnectionProperties(Properties connectionProperties) {
		this.connectionProperties = connectionProperties;
	}

	
	/**
	 * @see com.bourse.core.ibatis.SqlMapClientFactory#newSqlMapClient()
	 */
	public SqlMapClient newSqlMapClient() throws SqlMapClientInstanciationException {
		try {
			final Reader reader = Resources.getResourceAsReader(this.configuration);
			return SqlMapClientBuilder.buildSqlMapClient(reader);
		} catch (IOException e) {
			throw new SqlMapClientInstanciationException("Unable to create SqlMapClient", e);
		}
	}

	public SqlMapClient newSqlMapClientWithProperties() throws SqlMapClientInstanciationException {
		try {
			final Reader reader = Resources.getResourceAsReader(this.configuration);
			return SqlMapClientBuilder.buildSqlMapClient(reader, this.connectionProperties);
		} catch (IOException e) {
			throw new SqlMapClientInstanciationException("Unable to create SqlMapClient", e);
		}
	}

}
